package com.yang.dao.impl;

import com.yang.dao.SelectInformationDao;
import com.yang.entity.GuestEntity;
import com.yang.entity.LiverRelationEntity;
import com.yang.entity.OrderFormEntity;
import com.yang.entity.RoomEntity;
import com.yang.pojo.GuestDto;
import com.yang.pojo.ModificationDto;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Repository
public class SelectInformationDtoImpl implements SelectInformationDao {

    private static Logger logger = Logger.getLogger(SelectInformationDtoImpl.class);

    @Autowired
    HibernateTemplate hibernateTemplate;

    @Autowired
    SessionFactory sessionFactory;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<GuestEntity> selectGuestByNam(String name) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(GuestEntity.class);
        criteria.add(Restrictions.eq("name",name));
        List<GuestEntity> guestEntities = criteria.list();
        session.close();
        return guestEntities;
    }

    @Override
    public LiverRelationEntity selectLiverByguesIdAndStatus(long guestId, String status) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(LiverRelationEntity.class);
        criteria.add(Restrictions.eq("guestId",guestId));
        criteria.add(Restrictions.eq("status",status));
        LiverRelationEntity liverRelationEntity = (LiverRelationEntity) criteria.uniqueResult();
        session.close();
        return liverRelationEntity;
    }

    @Override
    public List<LiverRelationEntity> selectLiveByRoomIdAndStatus(long roomId, String status) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(LiverRelationEntity.class);
        criteria.add(Restrictions.eq("roomId",roomId));
        criteria.add(Restrictions.eq("status",status));
        List<LiverRelationEntity> liverRelationEntities = criteria.list();
        session.close();
        return liverRelationEntities;
    }

    @Override
    public GuestEntity selectGuestByGuestId(long guestId) {
//        Session session = sessionFactory.openSession();
//        Criteria criteria = session.createCriteria(GuestEntity.class);
//        criteria.add(Restrictions.eq("id",guestId));
//        GuestEntity guestEntity = (GuestEntity) criteria.uniqueResult();
//        session.close();
        String hql = "FROM GuestEntity WHERE id = ?";
        List<GuestEntity> guestEntities = (List<GuestEntity>) hibernateTemplate.find(hql,guestId);
        return guestEntities.isEmpty()?null:guestEntities.get(0);
    }

    @Override
    public OrderFormEntity selectOrderByOrderId(long orderId) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(OrderFormEntity.class);
        criteria.add(Restrictions.eq("id",orderId));
        OrderFormEntity orderFormEntity = (OrderFormEntity) criteria.uniqueResult();
        session.close();
        return orderFormEntity;
    }

    @Override
    public RoomEntity selectRoomByRoomNum(String roomNum) {
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(RoomEntity.class);
        criteria.add(Restrictions.eq("roomNum",roomNum));
        RoomEntity roomEntity = (RoomEntity) criteria.uniqueResult();
        session.close();
        return roomEntity;
    }

    @Override
    public List<ModificationDto> selectModByRoomAndName(String room, final String name) {
        String sql = "SELECT id,name,tel,idetification FROM guest WHERE name = ? AND id IN (SELECT guest_id FROM liver_relation WHERE room_id IN (SELECT id FROM room WHERE room_num = ?));";
        ResultSetExtractor<List<ModificationDto>> lists = new ResultSetExtractor<List<ModificationDto>>() {
            @Override
            public List<ModificationDto> extractData(ResultSet rs) throws SQLException, DataAccessException {
                List<ModificationDto> lists = new ArrayList<ModificationDto>();
                while(rs.next()) {
                    ModificationDto modificationDto = new ModificationDto();
                    modificationDto.setId(String.valueOf(rs.getLong("id")));
                    modificationDto.setName(rs.getString("name"));
                    modificationDto.setIdentification(rs.getString("idetification"));
                    modificationDto.setTel(rs.getString("tel"));
                    logger.info("modificationDto = "+modificationDto);
                    lists.add(modificationDto);
                }
                return lists;
            }
        };
        return jdbcTemplate.query(sql,new Object[] {name,room},lists);
    }

    @Override
    public boolean updataGuest(GuestEntity guestEntity) {
        try {
            hibernateTemplate.update(guestEntity);
        }catch (DataAccessException e){
            e.printStackTrace();
            logger.error("guest表更新失败");
            logger.error(guestEntity);
            return false;
        }
        return true;
    }
}
